<?php
class MY_Controller extends CI_Controller{

	function __construct(){

		parent::__construct();
		date_default_timezone_set('Asia/Shanghai');
	}

	//获得登录用户的ID
	public function get_user_id(){

		$userId = $this->input->cookie('userId',true);
		$userName = $this->input->cookie('userName',true);
		if(!isset($userId) || empty($userId)){
			$url = $this->_get_current_url();
			redirect('login?redirect='.$url);
		}

		return $userId;
	}

	public function _get_current_url(){

		$pageURL = 'http';
		if($_SERVER['HTTPS'] == 'on'){
			$pageURL .= 's';
		}

		$pageURL .= '://';
		if($_SERVER['SERVER_PORT'] != '80'){
			$pageURL .= $_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI'];
		}else{
			$pageURL .= $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
		}

		return $pageURL;
	}

	public function get_current_url(){
		$pageURL = 'http';
		$pageURL .= '://';
		if($_SERVER['SERVER_PORT'] != '80'){
			$pageURL .= $_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI'];
		}else{
			$pageURL .= $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
		}

		return $pageURL;

	}

	public function check_login_status(){
		$userId = $this->input->cookie('userId',true);
		$userName = $this->UsersModel->get_name_by_id($userId);
		$url = $this->get_current_url();
		$result = array(
			'userId' => $userId,
			'userName' => $userName,
			'url' => $url
		);

		return $result;
	}
}
